package cn.kimming.wms.web.action;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;

import cn.kimming.wms.anno.RequiredPermission;
import cn.kimming.wms.domain.JsonMsg;
import cn.kimming.wms.domain.OrderBill;
import cn.kimming.wms.query.OrderBillQueryObject;
import cn.kimming.wms.service.IOrderBillService;
import cn.kimming.wms.service.ISupplierService;
import lombok.Getter;

@Controller("orderBillAction")
@Scope("prototype")
public class OrderBillAction extends BaseAction {

	private static final long serialVersionUID = -8784596193420730799L;

	@Autowired
	private IOrderBillService orderBillService;
	@Autowired
	private ISupplierService supplierService;

	@Getter
	private OrderBill orderBill = new OrderBill();

	@Getter
	private OrderBillQueryObject qo = new OrderBillQueryObject();

	@Override
	@RequiredPermission("采购订单列表")
	@InputConfig(methodName = "input")
	public String execute() {
		try {
			ActionContext.getContext().put("objs", orderBillService.query(qo));
			ActionContext.getContext().put("suppliers", supplierService.list());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return LIST;
	}

	@RequiredPermission("采购订单编辑")
	public String input() {
		try {
			ActionContext.getContext().put("suppliers", supplierService.list());
			if (orderBill.getId() != null) {
				orderBill = orderBillService.get(orderBill.getId());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return INPUT;
	}

	@RequiredPermission("采购订单查看")
	public String show() {
		try {
			if (orderBill.getId() != null) {
				orderBill = orderBillService.get(orderBill.getId());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "show";
	}

	@RequiredPermission("采购订单保存和更新")
	public String saveOrUpdate() {
		if (orderBill.getId() != null) {
			try {
				orderBillService.update(orderBill);
				addActionMessage("更新成功!");
			} catch (Exception e) {
				e.printStackTrace();
				addActionError("系统异常,更新失败!");
			}
		} else {
			try {
				orderBillService.save(orderBill);
				addActionMessage("保存成功!");
			} catch (Exception e) {
				e.printStackTrace();
				addActionError("系统异常,保存失败!");
			}
		}
		return SUCCESS;
	}

	@RequiredPermission("采购订单删除")
	public String delete() {
		try {
			if (orderBill.getId() != null) {
				orderBillService.delete(orderBill.getId());
				putJSON(new JsonMsg(true, "删除成功"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			putJSON(new JsonMsg(false, "系统异常,删除失败!"));
		}
		return NONE;
	}

	@RequiredPermission("采购订单审核")
	public String audit() {
		try {
			if (orderBill.getId() != null) {
				orderBillService.audit(orderBill.getId());
				putJSON(new JsonMsg(true, "审核成功"));
			}
		} catch (NullPointerException e) {
			putJSON(new JsonMsg(false, e.getMessage()));
		} catch (Exception e) {
			e.printStackTrace();
			putJSON(new JsonMsg(false, "系统异常,审核失败!"));
		}
		return NONE;
	}

	public void prepareSaveOrUpdate() throws Exception {
		if (orderBill.getId() != null) {
			orderBill = orderBillService.get(orderBill.getId());
			orderBill.setSupplier(null);
		}
		orderBill.getItems().clear();
	}
}
